<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="会员ID" prop="memberId">
      <el-input v-model="dataForm.memberId" placeholder="会员ID"></el-input>
    </el-form-item>
    <el-form-item label="账户余额变动数（正数）" prop="balance">
      <el-input v-model="dataForm.balance" placeholder="账户余额变动数（正数）"></el-input>
    </el-form-item>
    <el-form-item label="修改类型：1新增，2减少" prop="changeType">
      <el-input v-model="dataForm.changeType" placeholder="修改类型：1新增，2减少"></el-input>
    </el-form-item>
    <el-form-item label="操作后账户余额" prop="afterBalance">
      <el-input v-model="dataForm.afterBalance" placeholder="操作后账户余额"></el-input>
    </el-form-item>
    <el-form-item label="操作类型" prop="operType">
      <el-input v-model="dataForm.operType" placeholder="操作类型"></el-input>
    </el-form-item>
    <el-form-item label="应用ID" prop="appId">
      <el-input v-model="dataForm.appId" placeholder="应用ID"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="" prop="creator">
      <el-input v-model="dataForm.creator" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="记录时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="记录时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="updater">
      <el-input v-model="dataForm.updater" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="源数据产生类型：order:订单：device:设备" prop="souceType">
      <el-input v-model="dataForm.souceType" placeholder="源数据产生类型：order:订单：device:设备"></el-input>
    </el-form-item>
    <el-form-item label="原数据ID" prop="sourceId">
      <el-input v-model="dataForm.sourceId" placeholder="原数据ID"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          memberId: '',
          balance: '',
          changeType: '',
          afterBalance: '',
          operType: '',
          appId: '',
          remark: '',
          creator: '',
          createTime: '',
          updater: '',
          updateTime: '',
          souceType: '',
          sourceId: ''
        },
        dataRule: {
          memberId: [
            { required: true, message: '会员ID不能为空', trigger: 'blur' }
          ],
          balance: [
            { required: true, message: '账户余额变动数（正数）不能为空', trigger: 'blur' }
          ],
          changeType: [
            { required: true, message: '修改类型：1新增，2减少不能为空', trigger: 'blur' }
          ],
          afterBalance: [
            { required: true, message: '操作后账户余额不能为空', trigger: 'blur' }
          ],
          operType: [
            { required: true, message: '操作类型不能为空', trigger: 'blur' }
          ],
          appId: [
            { required: true, message: '应用ID不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '记录时间不能为空', trigger: 'blur' }
          ],
          updater: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          souceType: [
            { required: true, message: '源数据产生类型：order:订单：device:设备不能为空', trigger: 'blur' }
          ],
          sourceId: [
            { required: true, message: '原数据ID不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/disk/memberbalancelog/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberId = data.memberBalanceLog.memberId
                this.dataForm.balance = data.memberBalanceLog.balance
                this.dataForm.changeType = data.memberBalanceLog.changeType
                this.dataForm.afterBalance = data.memberBalanceLog.afterBalance
                this.dataForm.operType = data.memberBalanceLog.operType
                this.dataForm.appId = data.memberBalanceLog.appId
                this.dataForm.remark = data.memberBalanceLog.remark
                this.dataForm.creator = data.memberBalanceLog.creator
                this.dataForm.createTime = data.memberBalanceLog.createTime
                this.dataForm.updater = data.memberBalanceLog.updater
                this.dataForm.updateTime = data.memberBalanceLog.updateTime
                this.dataForm.souceType = data.memberBalanceLog.souceType
                this.dataForm.sourceId = data.memberBalanceLog.sourceId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/disk/memberbalancelog/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'memberId': this.dataForm.memberId,
                'balance': this.dataForm.balance,
                'changeType': this.dataForm.changeType,
                'afterBalance': this.dataForm.afterBalance,
                'operType': this.dataForm.operType,
                'appId': this.dataForm.appId,
                'remark': this.dataForm.remark,
                'creator': this.dataForm.creator,
                'createTime': this.dataForm.createTime,
                'updater': this.dataForm.updater,
                'updateTime': this.dataForm.updateTime,
                'souceType': this.dataForm.souceType,
                'sourceId': this.dataForm.sourceId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
